package com.waypolice.manager.controller;


import com.waypolice.base.BaseController;
import com.waypolice.common.utils.DateTimeUtil;
import com.waypolice.pojo.*;
import com.waypolice.manager.service.UpmsArchiveScopeService;
import com.waypolice.manager.service.UpmsDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 归档范围管理controller
 * @author  lzh on 2018/3/29.
 */
@Controller
@Api(value = "归档范围", description = "归档范围")
@RequestMapping("/manage/scope")
public class UpmsArchiveScopeController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsArchiveScopeController.class);

    @Autowired
    private UpmsArchiveScopeService upmsArchiveScopeService;

    @Autowired
    private UpmsDictionaryService upmsDictionaryService;


    @ApiOperation(value = "查询归档范围")
    @RequiresPermissions("upms:archivestype:read")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<UpmsArchiveScope> list(Integer archivesId){
        UpmsArchiveScopeExample scopeExample = new UpmsArchiveScopeExample();
        scopeExample.createCriteria().andArchivesIdEqualTo(archivesId);
        List<UpmsArchiveScope> upmsArchiveScopes = upmsArchiveScopeService.selectByExample(scopeExample);
        return upmsArchiveScopes;
    }
    @ApiOperation(value = "添加归档范围")
    @RequiresPermissions("upms:scope:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Integer create(UpmsArchiveScope scope){
        int count = 0;
        if(scope!=null){
            try {
                Date cTime = DateTimeUtil.getNowDate();
                scope.setCtime(cTime);
            }catch (Exception e){

            }
            count = upmsArchiveScopeService.insert(scope);
        }
        return count;

    }
    @ApiOperation(value = "修改归档范围")
    @RequiresPermissions("upms:scope:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Integer update(Integer scopeId,String scopeName,String retention){
        int count = 0;
        if(scopeId!=null && scopeName !=null){
            UpmsArchiveScope upmsArchiveScope = upmsArchiveScopeService.selectByPrimaryKey(scopeId);
            upmsArchiveScope.setScopeName(scopeName);
            upmsArchiveScope.setRetention(retention);
            try {
                Date mTime = DateTimeUtil.getNowDate();
                upmsArchiveScope.setMtime(mTime);
            }catch (Exception e){

            }
            count = upmsArchiveScopeService.updateByPrimaryKey(upmsArchiveScope);
        }
        return count;

    }
    @ApiOperation(value = "查询保管年限")
    @RequestMapping(value = "/preservationDate", method = RequestMethod.GET)
    @ResponseBody
    public List<UpmsDictionary> selctDate(String code){
        UpmsDictionaryExample upmsDictionaryExample = new UpmsDictionaryExample();

        upmsDictionaryExample.or().andFatherCodeEqualTo(code);
        List<UpmsDictionary> upmsDictionaries = upmsDictionaryService.selectByExample(upmsDictionaryExample);
        return upmsDictionaries;
    }

    @ApiOperation(value = "删除归档范围")
    @RequiresPermissions("upms:scope:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Integer delScope(Integer scopeId){
        int count = 0;
        if (scopeId !=null) {
            int i = upmsArchiveScopeService.deleteByPrimaryKey(scopeId);
            count+=i;
        }
        return count;
    }


}